/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.SaleQuotation;

import entity.SaleQuotation;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SaleQuotationBeanLocal;

/**
 *
 * @author Jing Guo
 */
@ManagedBean(name="editSaleQuotationManagerBean")
@ViewScoped
public class EditSaleQuotationManagerBean {

    /** Creates a new instance of editSaleQuotationManagerBean */
    public EditSaleQuotationManagerBean() {
    }
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    private SaleQuotation saleQuotationToEdit;
    private String statusMessage;
    
    public SaleQuotation getSaleQuotationToEdit(){
        return saleQuotationToEdit;
    }
    
    public void setSaleQuotationToEdit(SaleQuotation saleQuotation){
        this.saleQuotationToEdit = saleQuotation;
    }
    
    public void initView(PhaseEvent event){
        saleQuotationToEdit=(SaleQuotation)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleQuotation");
        if(saleQuotationToEdit==null){
            System.out.print("No Sale Quotation");
        }
    }
    
    public void updateSaleQuotation(ActionEvent event){
        try{
            saleQuotationBeanLocal.updateSaleQuotation(saleQuotationToEdit.getId(), saleQuotationToEdit.getLineItems());
            statusMessage = "Sale Quotation Updated";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Sale Quotation Result: " + statusMessage + " (Sale Quotation ID is " + saleQuotationToEdit.getId().toString() + ")", ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
        
}
